package com.systop.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.systop.config.AlipayTemplate;
import com.systop.domain.vo.PayVo;
import com.systop.entity.Order;
import com.systop.service.OrderService;
import com.systop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: TODO
 * @Author: jsj
 * @Date: 2023/3/24
 */
@Controller
public class PayWebController {

    @Autowired
    AlipayTemplate alipayTemplate;

    @Autowired
    OrderService orderService;

    @GetMapping(value = "/payOrder"/*,produces = MediaType.TEXT_HTML_VALUE*/)
    @ResponseBody
    public R payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
        List<Order> orderList =
                orderService.list(new QueryWrapper<Order>().eq("order_no", orderSn));
        //应付金额
        Double payPrice = orderList.stream().map(Order::getSumPrice).reduce(Double::sum).get();
        String subject = "订单号:"+orderSn;
        //封装数据
        PayVo payVo = new PayVo();
        payVo.setOut_trade_no(orderSn);
        payVo.setTotal_amount(String.valueOf(payPrice));
        payVo.setSubject(subject);
        payVo.setBody(subject);

        return R.ok().put("aliPayUrl", alipayTemplate.pay(payVo));
        // return alipayTemplate.pay(payVo);
    }

}
